//
//  UploadImageVC.m
//  ZhongXiangGame
//
//  Created by Dubaoquan on 2019/3/14.
//  Copyright © 2019 阿里巴巴集团. All rights reserved.
//

#import "UploadOnlyOneImageVC.h"
#import "ZHBUpLoadManager.h"
#import "RunTimeInit.h"

@interface UploadOnlyOneImageVC ()<ACSelectMediaViewDelegate>
@property(nonatomic,strong) NSString *uploadType;//1上传头像
@property(nonatomic,strong) NSArray<ACMediaModel *> *imagelist;
@end

@implementation UploadOnlyOneImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if([self.uploadType isEqualToString:@"1"]){
        self.navigationItem.title = @"头像上传";
    }
    
    WEAK_SELF(weakSelf)
    CGFloat Y = 0;
    if (@available(iOS 11.0, *)) {
        Y = 180;
    } else {
        Y = 100;
    }
    CGFloat height = SCREENWIDTH/4.0;
    SelectImageOrVideoContentView *mediaView = [RunTimeInit runTimeInitWithClassName:@"SelectImageOrVideoContentView" propertyNamesAndValues:@{@"frameValue":[NSValue valueWithCGRect:CGRectMake(10,Y, SCREENWIDTH-20, height)]}];
    mediaView.delegate = self;
    mediaView.showDelete = YES;
    mediaView.showAddButton = YES;
    mediaView.allowMultipleSelection = NO;
    mediaView.allowPickingVideo = NO;
    mediaView.maxImageSelected = 1;
    
    [mediaView observeViewHeight:^(CGFloat mediaHeight) {
        
    }];
    
    [mediaView observeSelectedMediaArray:^(NSArray<ACMediaModel *> *list) {
        
        STRONG_SELF(strongSelf)
        strongSelf.imagelist = list;
        [UIApplication sharedApplication].statusBarHidden = NO;
    }];
    [self.view addSubview:mediaView];
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.backgroundColor = MainColor;
    [submitButton addTarget:self action:@selector(submitButtonAction) forControlEvents:UIControlEventTouchUpInside];
    submitButton.frame = CGRectMake((SCREENWIDTH-200)/2.0, CGRectGetMaxY(mediaView.frame)+40, 200, 40);
    submitButton.clipsToBounds = YES;
    [submitButton setTitle:@"开始上传" forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    submitButton.titleLabel.font = BigFont;
    submitButton.layer.cornerRadius = 10;
    [self.view addSubview:submitButton];
    [[ZHBUpLoadManager sharedInstance] prepareWithTokenType:@"common"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (@available(iOS 11.0, *)) {
        self.navigationController.navigationBar.prefersLargeTitles = YES;
        self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
    } else {
        // Fallback on earlier versions
    }
    
}
-(void)selectMediaViewTaped{
    [self.view endEditing:YES];
}

-(void)submitButtonAction{
    if (self.imagelist.count>0) {
        ACMediaModel *model = self.imagelist.firstObject;
        
        if([self.uploadType isEqualToString:@"1"]){
            
            [[ZHBUpLoadManager sharedInstance] uploadWithOutUseQINIUForImage:model.image imagePostKeyName:@"avatar" fileName:model.name  postURL:[NSString stringWithFormat:@"%@api/app/user/avatar",ServerNewIP] parameters:nil notificationName:@"UserInfoChangedTooUpdateUINotification"];
            
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
